<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品编辑</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <script src="/lib/jquery/1.9.1/jquery.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="/icheck-1.x/icheck.js"></script>
    <script src="/bootstrap-switch-master/dist/js/bootstrap-switch.js"></script>
    <link href="/bootstrap-switch-master/dist/css/bootstrap3/bootstrap-switch.css" rel="stylesheet">
    <link href="/icheck-1.x/skins/all.css" rel="stylesheet">
    <link href="/H-ui.admin/lib/webuploader/0.1.5/webuploader.css" rel="stylesheet" type="text/css" >
    <script src="/H-ui.admin/lib/webuploader/0.1.5/webuploader.js"></script>
    <script src="/H-ui.admin/lib/layer/2.4/layer.js"></script>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
</head>
<body>
<form action="{:url('store.stayIn/edit')}" class="form-horizontal" method="post" style="margin: 25px 50px">
    <input type="text" value="{$item?$item.id:''}" name="id" hidden>
    <h4>基本信息</h4>
    <div class="form-group">
        <label class="col-sm-2 control-label">商户名称</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="store_name" value="{$item?$item.store_name:''}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">商户logo</label>
        <div class="col-sm-8">
            <div id="logoList" class="uploader-list">
                {notempty name="item.logo"}
                <div class="file-item thumbnail">
                    <img src="{$item.logo}" style="display: inline-block;width:100px;" class="logoFromServer" />
                    <em class="close">×</em>
                </div>
                {/notempty}
            </div>
            <span class="help-block">图片大小小于500kb</span>
            <div id="logo">选择图片</div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">主营项目</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="main_project" value="{$item?$item.main_project:''}"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">商户分类</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="store_type" value="{$item?$item.store_type:''}"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">管理员</label>
        <div class="col-sm-8">
            <select class="form-control" name="menber_id">
                <option selected value="">—————选择管理员—————</option>
                {foreach $users   as   $key   =>$value}
                <option value="{$value.uid}" {:!empty($item)&&$item.menber_id==$value.uid?'selected':''}>{$value.rname}</option>
                {/foreach}
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">联系人</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="user.rname" value="{$item.user?$item.user.rname:''}"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">联系电话</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="user.mobile" value="{$item?$item.user.mobile:''}"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">商户简介</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="intro" value="{$item?$item.intro:''}"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">商户地址</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="address" value="{$item?$item.address:''}"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">商户电话</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="mobile" value="{$item?$item.mobile:''}"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">商户位置</label>
        <div class="col-sm-8">
            <div class="col-sm-10">
                <div class="input-group">
                    <span class="input-group-addon">经度</span>
                    <input type="text" class="form-control" name="long" value="{$item?$item.long:''}"/>
                    <span class="input-group-addon">纬度</span>
                    <input type="text" class="form-control" name="lat" value="{$item?$item.lat:''}"/>
                </div>
            </div>
            <button type="button" class="btn btn-primary baiduMap">选择位置</button>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">是否推荐</label>
        <div class="col-sm-8">
            <div class="col-sm-10">
                <div class="radio-inline">
                    <input type="radio" name="is_recommend" value="1">&emsp;是
                </div>
                <div class="radio-inline">
                    <input type="radio" name="is_recommend" value="0">&emsp;否
                </div>
            </div>
        </div>
    </div>
    <h4>追加资料</h4>
    <div class="form-group">
        <label class="col-sm-2 control-label">地址</label>
        <div class="col-sm-8">
            <div class="col-sm-3">
                <select class="form-control" onchange="getCity()" name="province">
                    <option selected value=" ">--请选择省份--</option>
                    {foreach $provinces as $key   =>$value}
                    <option value="{$value.code}" {:isset($item)&&$item.province==$value.code?'selected':''}>{$value.name}</option>
                    {/foreach}
                </select>
            </div>
            <div class="col-sm-3">
                <select class="form-control" name="city" onchange="getArea()">
                    <option selected value=" ">--请选择城市--</option>
                    {notempty name="cities"}
                    {foreach $cities as $key   =>$value}
                    <option value="{$value.code}" {:isset($item)&&$item.city==$value.code?'selected':''}>{$value.name}</option>
                    {/foreach}
                    {/notempty}
                </select>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">街道</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" name="street" value="{$item?$item.street:''}" placeholder="填写店铺信息详细地址到号">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">营业执照</label>
        <div class="col-sm-8">
            <div id="licenseList" class="uploader-list">
                {notempty name="item.license"}
                <div class="file-item thumbnail">
                    <img src="{$item.license}" style="display: inline-block;width:100px;" class="licenseFromServer" />
                    <em class="close">×</em>
                </div>
                {/notempty}
            </div>
            <span class="help-block">图片大小小于500kb</span>
            <div id="license">选择图片</div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">经营许可证</label>
        <div class="col-sm-8">
            <div id="special_licenseList" class="uploader-list">
                {notempty name="item.special_license"}
                <div class="file-item thumbnail">
                    <img src="{$item.special_license}" style="display: inline-block;width:100px;" class="special_licenseFromServer" />
                    <em class="close">×</em>
                </div>
                {/notempty}
            </div>
            <span class="help-block">图片大小小于500kb</span>
            <div id="special_license">选择图片</div>
        </div>
    </div>
    <h4>账户信息</h4>
    <div class="form-group">
        <label class="col-sm-2 control-label">服务到期时间</label>
        <div class="col-sm-4">
            <input type="date" class="form-control" name="service_end" value="{$item.service_end?$item.service_end:''}">
        </div>
    </div>
    <h4>商户备注</h4>
    <div class="form-group">
        <label class="col-sm-2 control-label">商户备注</label>
        <div class="col-sm-8">
            <textarea style="width:100%;height: 200px;" name="remark" value="{$item.remark?$item.remark:''}"></textarea>
        </div>
    </div>
    <h4>入驻状态</h4>
    <div class="form-group">
        <label class="col-sm-2 control-label">状态</label>
        <div class="col-sm-8">
            <div class="radio-inline">
                <input type="radio" name="status" value="0">&emsp;待入驻
            </div>
            <div class="radio-inline">
                <input type="radio" name="status" value="1">&emsp;允许入驻
            </div>
            <div class="radio-inline">
                <input type="radio" name="status" value="2">&emsp;暂停中
            </div>
        </div>
    </div>
    <h4>商户结算</h4>
    <div class="form-group">
        <label class="col-sm-2 control-label">抽成比例</label>
        <div class="col-sm-8">
            <div class="input-group">
                <input type="text" class="form-control" name="rate" value="{$item.rate?$item.rate:''}">
                <span class="input-group-addon">%</span>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">收款人</label>
        <div class="col-sm-8">
            <div class="input-group">
                <input type="text" class="form-control" name="rece_uid" value="{$item.receUser?$item.receUser.rname:''}">
                <span class="input-group-btn">
                    <button type="button" class="btn btn-primary">选择收款人</button>
                </span>
            </div>

        </div>
    </div>

    {notempty name="controller"}
    <h4>申请操作</h4>
    <div class="form-group">
        <label class="col-sm-2 control-label">申请状态</label>
        <div class="col-sm-8">
            <div class="radio-inline">
                <input type="radio" name="apply_status" value="0">&emsp;申请中
            </div>
            <div class="radio-inline">
                <input type="radio" name="apply_status" value="1">&emsp;驳回申请
            </div>
        </div>
    </div>
    {/notempty}

    <div class="form-group">
        <label class="col-sm-4 control-label"></label>
        <div class="col-sm-2">
            <button type="submit" id="ctlBtn" class="btn btn-primary" style="float: left;">&emsp;提交&emsp;</button>
            <button type="button" class="btn btn-default" style="float: right;" onclick="history.back();">&emsp;返回&emsp;</button>
        </div>
    </div>
</form>

<script>
    //iframe窗
    function open(obj){
        //iframe层

        var index=layer.open({
            type: 2,
            title: obj.title,
            shadeClose: false,
            shade: 0.3,
            area: ['80%', '80%'],
            content: obj.url, //iframe的url
            end:obj.end
        });
        return index;
    }

    function getArea(){
        var code=$("select[name='city'] option:selected").val();
        if(code==" "){
            $("select[name='area']").empty();
            $("select[name='area']").append($("<option value=''>--请选择--</option>"));
            return;
        }
        $.ajax({
            url:"{:url('getCity')}",
            type:"post",
            data:{
                "code":code
            },
            success:function (res) {
                if(res.code==200){
                    //console.log(res);
                    $("select[name='area']").empty();
                    $.each(res.data,function (index, item) {
                        $("select[name='area']").append($("<option value="+item.code+">"+item.name+"</option>"));
                    });

                }else{
                    console.log(res);
                }
            }
        });
    }

    function getCity(){
        var code=$("select[name='province'] option:selected").val();
        if(code==" "){
            $("select[name='city']").empty();
            $("select[name='area']").empty();
            $("select[name='city']").append($("<option value=''>--请选择--</option>"));
            $("select[name='area']").append($("<option value=''>--请选择--</option>"));
            return;
        }
        $.ajax({
            url:"{:url('getCity')}",
            type:"post",
            data:{
                "code":code
            },
            success:function (res) {
                if(res.code==200){
                    //console.log(res);
                    $("select[name='city']").empty();
                    $.each(res.data,function (index, item) {
                        $("select[name='city']").append($("<option value="+item.code+">"+item.name+"</option>"));
                    });

                }else{
                    console.log(res);
                }
            }
        });
    }

    function getArea(){
        var code=$("select[name='city'] option:selected").val();
        if(code==" "){
            $("select[name='area']").empty();
            $("select[name='area']").append($("<option value=''>--请选择--</option>"));
            return;
        }
        $.ajax({
            url:"{:url('getCity')}",
            type:"post",
            data:{
                "code":code
            },
            success:function (res) {
                if(res.code==200){
                    //console.log(res);
                    $("select[name='area']").empty();
                    $.each(res.data,function (index, item) {
                        $("select[name='area']").append($("<option value="+item.code+">"+item.name+"</option>"));
                    });

                }else{
                    console.log(res);
                }
            }
        });
    }

    function uploadImage(obj){
        var uploader1 = WebUploader.create({
            auto:true,
            // swf文件路径
            swf: '/H-ui.admin/lib/webuploader/0.1.5/Uploader.swf',

            // 文件接收服务端。
            server: obj.url,

            //设置文件上传域的name
            //fileVal:"cover",

            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: "#"+obj.pick,

            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize:false,

            //上传并发数。允许同时最大上传进程数
            threads:1,

            //fileNumLimit:1,

            //fileSizeLimit:500,

            fileSingleSizeLimit:500*1024,

            // 只允许选择图片文件。
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            }
        });
        uploader1.on("fileQueued",function (file) {
            var $li = $(
                '<div id="' + file.id + '" class="file-item thumbnail">' +
                '<img style="display: inline-block;width:100px" class="'+obj.pick+'FromServer">' +
                '<div class="info">' + file.name + '</div>' +
                '<em class="close">×</em>'+
                '</div>'

                ),
                $img = $li.find('img');

            $("."+obj.pick+"FromServer").parent().remove();
            // $list为容器jQuery实例
            $("#"+obj.pick+"List").append( $li );

            // 创建缩略图
            // 如果为非图片文件，可以不用调用此方法。
            // thumbnailWidth x thumbnailHeight 为 100 x 100
            uploader1.makeThumb( file, function( error, src ) {
                if ( error ) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }
                $img.attr( 'src', src );
            }, 100, 100 );
        });
        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader1.on( 'uploadSuccess', function( file,res ) {
            console.log(res);
            $( '#'+file.id ).addClass('upload-state-done');
            if(res.code==200){
                $( '#'+file.id+" img").attr("src",res.url);
                //coverArr.push(res.url);
            }else{
                console.log(res.errmsg);
            }
        });

        // 文件上传失败，显示上传出错。
        uploader1.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                $error = $li.find('div.error');

            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }

            $error.text('上传失败');
        });
        return uploader1;
    }

    $("input:not('.make-switch')").iCheck({
        labelHover : false,
        cursor : true,
        checkboxClass : 'icheckbox_square-blue',
        radioClass : 'iradio_square-blue',
        increaseArea : '20%'
    });

    var upload1=uploadImage({
        url:"{:url('Upload/uploadOne')}",
        pick:"logo"
    });
    var upload2=uploadImage({
        url:"{:url('Upload/uploadOne')}",
        pick:"license"
    });
    var upload3=uploadImage({
        url:"{:url('Upload/uploadOne')}",
        pick:"special_license"
    });

    $("#ctlBtn").on("click",function (e) {
        var logoArr=[],licenseArr=[],special_licenseArr=[];
        e.preventDefault();
        var form=new FormData($("form").get(0));
        $(".logoFromServer").each(function (index) {
            logoArr.push($(this).attr("src"));
        });
        $(".licenseFromServer").each(function (index) {
            licenseArr.push($(this).attr("src"));
        });
        $(".special_licenseFromServer").each(function (index) {
            special_licenseArr.push($(this).attr("src"));
        });

        var logostr=logoArr[0],licensestr=licenseArr[0],special_licensestr=special_licenseArr[0];
        form.append("logo",logostr);
        form.append("license",licensestr);
        form.append("special_license",special_licensestr);
        $.ajax({
            url:"{:url('store.stayIn/edit')}",
            type:"post",
            data:form,
            processData: false,  // 不处理数据
            contentType: false,   // 不设置内容类型
            success:function (res) {
                console.log(res);
            }
        })
    });

    $(document).on("click",".close",function (e) {
        $(this).parent().remove();
    });

    $(".baiduMap").on("click",function (e) {
        open({
            url:"{:url('map/index')}",
            title:"百度地图",
            end:function () {

            }
        });

    });
</script>
</body>
</html>